package com.miracle9.game.dao;

import com.miracle9.game.entity.Desk;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DeskServiceDao extends SqlOperating<Desk, Integer> {

    public List<Integer> getDeskIdsByRoomId(int roomId) {
        return (List<Integer>) getSession().createQuery("select id from Desk where roomId = ?").setParameter(0, roomId).list();
    }

    @Cacheable(value = {"springCache"}, key = "'getAllDesk'")
    public List<Desk> getAllDesk() {
        return q_CreateSqlList("from Desk order by roomId desc,orderBy", new Object[0]);
    }

    @Cacheable(value = {"springCache"}, key = "'getDesk'+#id")
    public Desk getDesk(int id) {
        return (Desk) getById(Integer.valueOf(id));
    }

    @Cacheable(value = {"springCache"}, key = "'countDesk'+#roomId")
    public int getQuantityByRoomId(int roomId) {
        return q_CreateSqlInt("select count(*) from Desk where roomId=?", new Object[]{Integer.valueOf(roomId)});
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getAllDesk'"), @CacheEvict(value = {"springCache"}, key = "'countDesk'+#desk.roomId")})
    public Integer saveReturnInteger(Desk desk) {
        return (Integer) saveReturn(desk);
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getDesk'+#desk.id"), @CacheEvict(value = {"springCache"}, key = "'getAllDesk'")})
    public void updateDeskInfo(Desk desk) {
        Desk update = getDesk(desk.getId());
        update.setRoomId(desk.getRoomId());
        update.setName(desk.getName());
        update.setMinGold(desk.getMinGold());
        update.setMinBet(desk.getMinBet());
        update.setMaxBet(desk.getMaxBet());
        update.setMin_zxh(desk.getMin_zxh());
        update.setMax_zx(desk.getMax_zx());
        update.setMax_h(desk.getMax_h());
        update.setBetTime(desk.getBetTime());
        update.setExchange(desk.getExchange());
        update.setOnceExchangeValue(desk.getOnceExchangeValue());
        update.setAnimalDiff(desk.getAnimalDiff());
        update.setZxhDiff(desk.getZxhDiff());
        update.setBeilvType(desk.getBeilvType());
        update.setBeilvModel(desk.getBeilvModel());
        update.setSiteType(desk.getSiteType());
        update.setWaterType(desk.getWaterType());
        update.setWaterValue(desk.getWaterValue());
        update.setAutoKick(desk.getAutoKick());
        update.setState(desk.getState());
        update.setBetChip(desk.getBetChip());
        update(update);
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getDesk'+#id"), @CacheEvict(value = {"springCache"}, key = "'getAllDesk'"), @CacheEvict(value = {"springCache"}, key = "'countDesk'+#roomId")})
    public void deleteById(int id, int roomId) {
        deleteById(Integer.valueOf(id));
    }

    @CacheEvict(value = {"springCache"}, key = "'getDesk'+#id")
    public void initializeById(int id) {
        q_CreateSql("update Desk set sumYaFen = 0,sumDeFen=0,sumZhxYaFen=0,sumZhxDeFen=0 where id=?", new Object[]{Integer.valueOf(id)}).executeUpdate();
    }

    @Caching(evict = {@CacheEvict(value = {"springCache"}, key = "'getAllDesk'")})
    public void updateOrderByById(int id, int index) {
        q_CreateSql("update Desk set orderBy = ? where id=?", new Object[]{Integer.valueOf(index), Integer.valueOf(id)}).executeUpdate();
    }

    public void updateStateById(int id, int state) {
        q_CreateSql("update Desk set state=? where id=?", new Object[]{Integer.valueOf(state), Integer.valueOf(id)}).executeUpdate();
    }

}
